package JDBC工具包;

import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @作者:侯国威
 * @时间: 2020/4/15/0:17
 * @邮箱:1045077189@qq.com 通用的增删改操作
 */
public class Insert_updata_deleat {
    public void update(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCPackage.getconnection();
            preparedStatement = connection.prepareStatement(sql);
            //填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);//参数声明错误
            }
            preparedStatement.execute();
        } catch (IOException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        JDBCPackage.closeResource(connection, preparedStatement);
    }

    //测试

    @Test
    public void testupdate(){
//        String sql="delete from star where id=?";
//        update(sql,5); 删除操作
        //更新操作

        String sql="update star set name=? where id=?";
        update(sql,"侯国威","5");
        System.out.println("1");
    }
}
